<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
    <meta name="referrer" content="never">
    <title>新动画函数</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }
        .box, .inner{
            position: absolute;
            top: 50px;
            left: 50px;
            width: 150px;
            height: 150px;
            background-color: #f00;
        }

        .inner {
            top: 230px;
        }
    </style>
</head>
<body>
    <div class="box"> box</div>
    <div class="inner"> inner</div>
    <script type="text/javascript">
        /* 
        var requestId = requestAnimationFrame(function)  请求动画帧，用于在浏览器重绘时执行函数
            接受参数函数默认带有一个参数 性能时间戳 => performance.now()

        cancelAnimationFrame(requestId) 取消请求动画帧
        */

        var box = document.querySelector(".box"),
            inner = document.querySelector(".inner");

        box.onclick = function (){
            var timer = setInterval(function (){
                box.style.left = box.offsetLeft + 1 + "px";
                if(box.offsetLeft == 600){
                    clearInterval(timer);
                }
            }, 1000/59);

            requestAnimationFrame(fn);
            function fn(){
                inner.style.left = inner.offsetLeft + 1 + "px";
                if(inner.offsetLeft < 600){
                    requestAnimationFrame(fn);
                }
            }
        }
    </script>
</body>
</html>